//package com.xiao.message.service.handler;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.rabbitmq.client.Channel;
//import com.xiao.foundation.errors.TencentResourceException;
//import com.xiao.client.blade.dto.UserInfoDto;
//import com.xiao.client.blade.feign.BladeUserClient;
//import com.xiao.client.common.feign.TencentClient;
//import com.xiao.message.service.dto.TpnsIosMessage;
//import com.xiao.message.service.dto.TpnsRequestBody;
//import com.xiao.message.service.dto.UserLoginDto;
//import com.xiao.message.service.dto.UserNotificationDto;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.ExchangeTypes;
//import org.springframework.amqp.support.AmqpHeaders;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.ResponseEntity;
//import org.springframework.messaging.handler.annotation.Header;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.client.RestTemplate;
//
//import java.time.LocalDateTime;
//import java.time.ZoneOffset;
//import java.util.Collections;
//
///**
// * @author mingyuezheng
// * 发货提醒
// */
//
//@Slf4j
//@Component
//
//@RabbitListener(bindings = @QueueBinding(
//        value = @Queue(value = "queue.message.notification.ios", durable = "true", autoDelete = "true"),
//        exchange = @Exchange(value = "exchange.fanout.message.notification", type = ExchangeTypes.FANOUT)), ackMode = "MANUAL")
//public class IosNotificationHandler extends BaseNotificationHandler {
//
//    private final TencentClient tencentClient;
//    private final StringRedisTemplate stringRedisTemplate;
//    private final BladeUserClient bladeUserClient;
//    private final String userLoginInfoKey = String.format("user:last:login:hash");
//
//    @Value("${tencent.cloud.tpns.ios-environment:}")
//    private String environment;
//    @Value("${tencent.cloud.tpns.ios-access-id:}")
//    private String iosAccessId;
//    @Value("${tencent.cloud.tpns.account-push-type:}")
//    private int accountPushType;
//    @Value("${tencent.cloud.tpns.account-type:}")
//    private int accountType;
//
//
//    public IosNotificationHandler(RestTemplate restTemplate, TencentClient tencentClient, StringRedisTemplate stringRedisTemplate, BladeUserClient bladeUserClient) {
//        super(restTemplate);
//        this.tencentClient = tencentClient;
//        this.stringRedisTemplate = stringRedisTemplate;
//        this.bladeUserClient = bladeUserClient;
//    }
//
//    @RabbitHandler
//    public void iosPushByAccount(UserNotificationDto userNotificationDto, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws Exception {
//        log.info("ios 消费：{}", userNotificationDto);
//
//        String userLoginInfoStr = (String) stringRedisTemplate.opsForHash().get(userLoginInfoKey, userNotificationDto.getUserId());
//        String equipmentSystem = null;
//        if (StringUtils.hasText(userLoginInfoStr)) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            UserLoginDto userLoginDto = objectMapper.readValue(userLoginInfoStr, UserLoginDto.class);
//            equipmentSystem = userLoginDto.getEquipmentSystem();
//        }
//
//        if (log.isDebugEnabled()) {
//            log.debug("userLoginInfoStr: {}", userLoginInfoStr);
//        }
//
//        boolean isOk = false;
//        if (!StringUtils.hasText(equipmentSystem) || equipmentSystem.equalsIgnoreCase("ios")) {
//            if (!StringUtils.hasText(userNotificationDto.getMobile())
//                    && StringUtils.hasText(userNotificationDto.getUserId())) {
//                UserInfoDto userInfoDto = bladeUserClient.getUserInfo(Long.valueOf(userNotificationDto.getUserId()));
//                userNotificationDto.setMobile(userInfoDto.getAccount());
//            }
//
//            String accessId = iosAccessId;
//            //构造请求体
//            TpnsIosMessage iosMessage = TpnsIosMessage.builder().title(userNotificationDto.getTitle()).content(userNotificationDto.getContent()).build();
//            TpnsRequestBody requestBody = TpnsRequestBody.builder().audience_type("account").account_type(accountType).account_push_type(accountPushType)
//                    .message(iosMessage).environment(environment).build();
//            requestBody.setAccount_list(Collections.singletonList(userNotificationDto.getMobile()));
//            ObjectMapper mapper = new ObjectMapper();
//            String requestStr = mapper.writeValueAsString(requestBody);
//            //时间秒数时间戳
//            long timeStamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//            ResponseEntity<String> responseEntity = tencentClient.signature("ios", timeStamp, requestStr);
//            String sign = responseEntity.getBody();
//
//            //APP推送
//            isOk = pushNotification(accessId, sign, requestStr, timeStamp);
//
//            channel.basicAck(deliveryTag, false);
//            if (!isOk) {
//                throw new TencentResourceException("message", "app ios 消息推送失败!", "notification", "mobilePushNotification");
//            }
//        }
//    }
//}